package cat.joronya.supermanager.service;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;
import cat.joronya.supermanager.Supermanager;

/**
 * Exposa l'API de metodes que podem cridar a la
 * web.
 * @author pol
 *
 */
public class SupermanagerServiceHelper 
{
	private static final String TAG = "supermanager";
	
	private static SupermanagerServiceHelper singleton = null;
	
	public static SupermanagerServiceHelper getInstance(ContextWrapper context)
	{
		if(singleton == null)
			singleton = new SupermanagerServiceHelper(context);
		
		return singleton;
	}
	
	private ContextWrapper contextWrapper;
	private String username;
	private String password;
	
	public SupermanagerServiceHelper(ContextWrapper context)
	{
		// el necessitem per crear els intents i per les prefs.
		contextWrapper = context;
		
		// mirem si tenim user/password a les preferences
   		SharedPreferences settings = 
   			contextWrapper.getSharedPreferences(Supermanager.SHARED_PREFS_NAME, 
   					Context.MODE_PRIVATE);
   		username = settings.getString("username", null);
   		password = settings.getString("password", null);
	}
	
	/**
	 * Fem login inicial, forçant l'us del user i password passats.
	 * @param username
	 * @param password
	 * @param handler
	 */
	public void login(String username, String password, Handler handler)
	{
		Log.d(TAG,"SupermanagerServiceHelper.login('"+username+"','"+password+"')");
		
		// guardem username i password al helper per proximes cride
		// si s'ha mort el service i cal reinicia la sessio
		this.username = username;
		this.password = password;
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(SupermanagerIntent.ACTION_LOGIN,
					Supermanager.Home.CONTENT_URI, contextWrapper, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		
		// executem l'intent al service
		contextWrapper.startService(intent);
	}

	/**
	 * Crida al service per recuperar de la web les lligues privades.
	 */
	public void getPrivateLeagues(Handler handler)
	{
		Log.d(TAG,"SupermanagerServiceHelper.getPrivateLeagues()");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Intent.ACTION_GET_CONTENT,
				Supermanager.PrivateLeague.CONTENT_URI, contextWrapper, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		
		// executem l'intent al service
		contextWrapper.startService(intent);
	}
	
	/**
	 * Crida al service per recuperar de la web les dades del mercat.
	 */
	public void getMarket(Handler handler)
	{
		Log.d(TAG,"SupermanagerServiceHelper.getMarket()");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Intent.ACTION_GET_CONTENT,
				Supermanager.Market.CONTENT_URI, contextWrapper, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		
		// executem l'intent al service
		contextWrapper.startService(intent);		
	}

	/**
	 * Crida al service per recuperar de la web els equips.
	 */
	public void getMyTeams(Handler handler)
	{
		Log.d(TAG,"SupermanagerServiceHelper.getMyTeams()");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Intent.ACTION_GET_CONTENT,
				Supermanager.MyTeam.CONTENT_URI, contextWrapper, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		
		// executem l'intent al service
		contextWrapper.startService(intent);
	}
	
}
